﻿if (!InfoSource) var InfoSource = {};
if (!InfoSource.SaveGeoCodesToDB) InfoSource.SaveGeoCodesToDB = {};

InfoSource.SaveGeoCodesToDB.Map;
InfoSource.SaveGeoCodesToDB.MapMarkers = {};
InfoSource.SaveGeoCodesToDB.GeoCoder;
InfoSource.SaveGeoCodesToDB.ZipCodeIndex = 0;

/*========================================================================================*/
InfoSource.SaveGeoCodesToDB.Init = function() {
	this.GeoCoder = new google.maps.Geocoder();
	window.setTimeout(InfoSource.SaveGeoCodesToDB.GeoCodeZipCode, 500);
}

/*======================================================================================*/
InfoSource.SaveGeoCodesToDB.InitHandlers = function() {

}

/*======================================================================================*/
InfoSource.SaveGeoCodesToDB.GeoCodeZipCode = function() {
	InfoSource.SaveGeoCodesToDB.GeoCoder.geocode({ address: InfoSource.SaveGeoCodesToDB.ZipCodes[InfoSource.SaveGeoCodesToDB.ZipCodeIndex] + " co" }
		, function(result, status) {
			var url = "/InfoSource/Settings/SaveSingleGeoCodeToDB";

			if (status == google.maps.GeocoderStatus.OK) {

				$.ajax({
					url: url
					, type: "POST"
					, data:
						{
							Zip: InfoSource.SaveGeoCodesToDB.ZipCodes[InfoSource.SaveGeoCodesToDB.ZipCodeIndex]
							, Lat: result[0].geometry.location.Ha
							, Long: result[0].geometry.location.Ia
							, Index: InfoSource.SaveGeoCodesToDB.ZipCodeIndex
						}
					, success: function(result) { $("#message").html(result + ": success"); }
				});

				if (InfoSource.SaveGeoCodesToDB.ZipCodeIndex < InfoSource.SaveGeoCodesToDB.ZipCodes.length) {
					InfoSource.SaveGeoCodesToDB.ZipCodeIndex += 1;
					window.setTimeout(InfoSource.SaveGeoCodesToDB.GeoCodeZipCode, 2000);
				}
			}
		});
}

$(document).ready(function() {
	InfoSource.SaveGeoCodesToDB.Init();
	InfoSource.SaveGeoCodesToDB.InitHandlers();
});